package com.easy.frame.servers.sys.controller.type;


import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.basic.exception.ServiceException;
import com.easy.frame.base.basic.msg.ExceptionMsg;
import com.easy.frame.base.basic.pojo.vo.BaseTreeVO;
import com.easy.frame.base.service.base.BaseController;
import com.easy.frame.servers.sys.dto.SysCategoryTypeDTO;
import com.easy.frame.servers.sys.entity.SysCategoryType;
import com.easy.frame.servers.sys.mapping.SysCategoryTypeMapping;
import com.easy.frame.servers.sys.service.type.SysCategoryTypeService;
import com.easy.frame.servers.sys.service.type.SysMaterielTypeService;
import com.easy.frame.servers.sys.vo.SysCategoryTypeVO;
import com.easy.frame.servers.sys.vo.SysMaterielTypeVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 物料分类表相关接口类
 *
 * @author tp
 * @date 2021/1/4 11:39
 */
@Slf4j
@Api(tags = "物料分类/sys-materiel")
@RestController
@RequestMapping("/sys-materiel")
public class SysMaterielTypeController extends BaseController {

    @Autowired
    SysMaterielTypeService sysMaterielTypeService;

    @Autowired
    SysCategoryTypeService sysCategoryTypeService;

    @ApiOperation(value = "树状图-获取物料分类树状图")
    @GetMapping("/tree")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否只查启用 [0全查 1只查启用]，默认查全部", name = "enabled"),
            @ApiImplicitParam(value = "查询条件，搜索组织树的条件", name = "equName"),
            @ApiImplicitParam(value = "分类类型 [5.物料分类]", name = "type", required = true)})
    public ServiceResponse<? extends BaseTreeVO> tree(@RequestParam(defaultValue = "0") int enabled,
                                                      @RequestParam String equName,
                                                      @RequestParam String type) {
        return success(sysMaterielTypeService.getEquTypeTree(enabled, equName, type));
    }

    @ApiOperation("点击新增一级分类/新增下级分类")
    @GetMapping("/click/add/loc")
    public ServiceResponse<SysCategoryTypeVO> clickAddEqu(@ApiParam("父级ID，为空时则增加的是一级分项，非空时将在该ID下新增分项")
                                                          @RequestParam(required = false) Integer parentLoId,
                                                          @RequestParam String type) {
        SysCategoryTypeVO sysCategoryTypeVO = sysMaterielTypeService.clickAddLoa(parentLoId, type);
        return success(sysCategoryTypeVO);
    }

    @ApiOperation("添加-物料分类")
    @PostMapping("/add")
    public ServiceResponse<SysCategoryType> add(@ApiParam("添加物料分类对象")
                                                @RequestBody SysCategoryTypeDTO addDTO) throws Exception {
        //处理父路径参数值，当父值不为空，或者删除头尾为空的时候；
        if (addDTO.getParentPath() != null && ("").equals(addDTO.getParentPath().trim())) {
            addDTO.setParentPath(null);
        }
        return success(sysMaterielTypeService.add(addDTO));
    }

    @ApiOperation("修改-物料分类")
    @PutMapping("/modify")
    public ServiceResponse<SysCategoryType> modify(@ApiParam("物料分类实体")
                                                   @RequestBody SysCategoryTypeDTO entity) {
        return success(sysMaterielTypeService.modify(entity));
    }

    @ApiOperation("查询-根据id")
    @GetMapping("/by/id")
    public ServiceResponse<?> getById(@ApiParam("主键id")
                                      @RequestParam Long id) {
        SysMaterielTypeVO sysMaterielTypeVO = SysCategoryTypeMapping.INSTANCE.categoryTypeVO2MaterielTypeVO(
                sysCategoryTypeService.selectById(id));

        if (StringUtils.isEmpty(sysMaterielTypeVO)) {
            return fail(ServiceException.parseInfo(ExceptionMsg.DONT_ALREADY_EXISTS));
        }
        return success(sysMaterielTypeVO);
    }

    @ApiOperation("删除-根据id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@RequestParam Long id) {
        //修改delete字段的值为1
        sysMaterielTypeService.updateDelete(id);
        return success();
    }

    @ApiOperation("删除父节点-根据id")
    @DeleteMapping("/parent/by/id")
    public ServiceResponse<?> deleteParentById(@RequestParam Long id) {
        //修改delete字段的值为1
        sysMaterielTypeService.updateParentDelete(id);
        return success();
    }

}
